﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThongTinNhanKhau.model.DAO
{
    class ChungTuDAO
    {
        SQLServiceDAO service = new SQLServiceDAO();

        internal System.Data.DataTable loadDefault(string maNhanKhau)
        {
            String sql = "select * from NHANKHAU where MaNhanKhau = '" + maNhanKhau + "'";
            DataTable dt = service.getDataTable(sql);
            return dt;
        }

        internal DataTable loadTable(string maNhanKhau)
        {
            String sql = "select MaChungTu, c.MaNhanKhau, HoTen, NgaySinh, NoiChet, NgayChet, NguyenNhan, NoiThuongTruCuoiCung from NHANKHAU n join ChungTu c on n.MaNhanKhau = c.MaNhanKhau where c.MaNhanKhau = '" + maNhanKhau + "' and c.TinhTrang = 'Enable'";
            DataTable dt = service.getDataTable(sql);
            return dt;
        }

        internal bool isValidMaCT(string str)
        {
            String sql = "Select TOP(1) * from ChungTu where MaChungTu = '" + str + "'";
            DataTable dt = service.getDataTable(sql);

            if (dt.Rows.Count != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        internal bool themMoiCT(string maCT, string maNhanKhau, string ngayChet, string noiChet, string nguyenNhan, string noiThuongTruCuoiCung)
        {
            String sql = "insert into ChungTu values ('" + maCT + "','" + maNhanKhau + "',N'" + noiChet + "','" + ngayChet + "',N'" + nguyenNhan + "',N'" + noiThuongTruCuoiCung + "','Enable')";
            return service.executeNonQuery(sql);
        }

        internal bool capNhatCT(string maCT, string maNhanKhau, string ngayChet, string noiChet, string nguyenNhan, string noiThuongTruCuoiCung)
        {
            String sql = "update CHUNGTU set NgayChet = '" + ngayChet + "', NoiChet = N'" + noiChet + "', NguyenNhan = N'" + nguyenNhan + "', NoiThuongTruCuoiCung = N'" + noiThuongTruCuoiCung + "' where MaChungTu = N'" + maCT + "' and MaNhanKhau = N'" + maNhanKhau + "'";
            return service.executeNonQuery(sql);
        }

        internal bool deleteRow(string maCT, string maNhanKhau)
        {
            String sql = "update CHUNGTU set TinhTrang = 'Disable' where MaChungTu = '" + maCT + "' and MaNhanKhau = '" + maNhanKhau + "'";
            return service.executeNonQuery(sql);
        }
    }
}
